using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;

namespace Norm.Helper
{
    /// <summary>
    /// You probly want Norm.Generic.Handle<>
    /// </summary>
    /// <typeparam name="T"></typeparam>
    public class SqlReader<T>
    {
        public T GetFirst(SqlDataReader reader)
        {
            if (!reader.HasRows)
                throw new Exception.UnableToRead();

            if(reader.Read())
            {
                T t = default(T);
                t = (T)SqlHelper.Convert(reader,Activator.CreateInstance(typeof(T)));
                return t;
            }
            
            throw new Exception.BlankObject();
        }

        public T[] Array(SqlDataReader reader)
        {
            ArrayList al = new ArrayList();
            T t;
            while ((t = GetFirst(reader)) != null)
                al.Add(t);

            T[] ts = new T[al.Count];
            for (int i = 0; i < al.Count; i++)
                ts[i] = (T)al[i];
            return ts;
        }
    }
}
